package helloWorld;

import java.util.List;
import java.util.Vector;
import helloWorld.infoRetriever.*;
import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.MTImage;

import processing.core.PImage;


public class ArtistScene extends AbstractScene{
	private IFont font1, font2;
	private Controller controller;
	private DBInterface dbInterface;
	private Vector<Artist> artists;
	private ArtistGestureListener myGL;
	private MTTextArea artistInfo;
	private MTRectangle artistPhoto;
	private MTImage goBackButton;
	
	public ArtistScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);
		myGL = new ArtistGestureListener(this.getMTApplication(), this);
		font1 = FontManager.getInstance().createFont(this.getMTApplication(), "arial.ttf", 
				50, 				  //Font size
				new MTColor(255, 255, 255, 255),   //Font fill color
				new MTColor(255, 255, 255, 255));
		font2 = FontManager.getInstance().createFont(this.getMTApplication(), "arial.ttf", 
				10, 				  //Font size
				new MTColor(255, 255, 255, 255),   //Font fill color
				new MTColor(146, 150, 188, 255));
		controller = Controller.getInstance();
		dbInterface = DBInterface.getInstance();
		artists = new Vector<Artist>();
		
		this.getCanvas().setFrustumCulling(true);
		this.setClearColor(new MTColor(146, 150, 188, 255));
		
		//prepare artist info
		artistInfo = new MTTextArea(this.getMTApplication(), font2);
		this.getCanvas().addChild(artistInfo);
		artistInfo.setPositionGlobal(new Vector3D(3f*mtApplication.width/4f, mtApplication.height/2f));
		
		// prepare artist image
		artistPhoto = new MTRectangle(new Vertex(400,300,0), 50,50, this.getMTApplication());
		this.getCanvas().addChild(artistPhoto);
		
		getCanvas().registerInputProcessor(new ZoomProcessor(getMTApplication()));
		getCanvas().addGestureListener(ZoomProcessor.class, myGL);
		
		String goBackUrl = "http://clipart.usscouts.org/library/BSA_Cub_Scouts/Cub_insignia/arrow_point_clipart_bw.gif";
		PImage pim = this.getMTApplication().loadImage(goBackUrl);
		goBackButton = new MTImage(pim, this.getMTApplication());
		goBackButton.setSizeXYGlobal(60, 60);
		goBackButton.setName("artistsceneBACK");
		goBackButton.unregisterAllInputProcessors();
		goBackButton.registerInputProcessor(new TapProcessor(getMTApplication()));
		goBackButton.addGestureListener(TapProcessor.class, myGL);
		this.getCanvas().addChild(goBackButton);
		
		goBackButton.setPositionGlobal(new Vector3D(getMTApplication().width/2, getMTApplication().height*9/10));
		
	}

	@Override
	public void init() {
		artists.clear();
		List<String> artistNames = dbInterface.captureArtistsWithLetter(controller.getCurrentLetter());
		for(int i = 0; i < artistNames.size(); ++i){
			Artist newArtist = new Artist(this.getMTApplication(), font1);
			newArtist.setNoFill(true);
			newArtist.setNoStroke(true);
			newArtist.setText(artistNames.get(i));
			newArtist.setName(artistNames.get(i));
			this.getCanvas().addChild(newArtist);
			newArtist.unregisterAllInputProcessors();
			newArtist.registerInputProcessor(new TapProcessor(this.getMTApplication()));
			newArtist.addGestureListener(TapProcessor.class, myGL);
			newArtist.registerInputProcessor(new ZoomProcessor(this.getMTApplication()));
			newArtist.addGestureListener(ZoomProcessor.class, myGL);
			newArtist.setPositionGlobal(new Vector3D(this.getMTApplication().width/5f, (i+3)*this.getMTApplication().height/10f));
			artists.add(newArtist);
		}
		
		getCanvas().registerInputProcessor(new ZoomProcessor(getMTApplication()));
		getCanvas().addGestureListener(ZoomProcessor.class, myGL);
		goBackButton.registerInputProcessor(new TapProcessor(getMTApplication()));
		goBackButton.addGestureListener(TapProcessor.class, myGL);
		
		
	}
	
	public void updateArtist(String artist){
		OnlineInfoRetriever oir = new OnlineInfoRetriever();
		oir.retrieveArtist(artist);
		
		String artInf = oir.getArtistInfo();
		String formatedInfo = "";
		boolean captureChar = true;
		int position = 0;
		for(int i =0; i < artInf.length();i++){
			if(position%30 == 0){
				formatedInfo+='\n';
			}
			
			if(artInf.charAt(i) ==  '<')
				captureChar = false;
			
			if(artInf.charAt(i) == '>'){
				captureChar = true;
				continue;
			}
			
			if(captureChar){
				formatedInfo+=artInf.charAt(i);
				position++;
			}
		}
		// clean out \n's
		String formatedInfo2 = "";
		char lastChar = formatedInfo.charAt(0);
		for(int i =1; i < formatedInfo.length();i++){
			if(formatedInfo.charAt(i) == '\n' && lastChar == '\n')
				;
			else{
				formatedInfo2+=formatedInfo.charAt(i);
			}
			lastChar = formatedInfo.charAt(i);
		}
		
		// update artist
		artistInfo.setFillColor(new MTColor(146, 150, 188, 255));
		artistInfo.setWidthLocal(300);
		artistInfo.setHeightLocal(100);
		artistInfo.setText(formatedInfo2);
		
		this.getCanvas().removeChild(artistInfo);
		this.getCanvas().addChild(artistInfo);
		
		// update image
		this.getCanvas().removeChild(artistPhoto);
		String imageUrl = oir.getArtistImage();
		PImage pim = this.getMTApplication().loadImage(imageUrl);
		artistPhoto = new MTImage(pim, this.getMTApplication());
		artistPhoto.setSizeXYGlobal(60, 60);
		this.getCanvas().addChild(artistPhoto);
		artistPhoto.setPositionGlobal(new Vertex(400,300,0));
		System.out.println("ARTISTINFO UPDATED!");
	}

	@Override
	public void shutDown() {
		for(int i = 0; i < artists.size(); ++i)
		{
			artists.get(i).unregisterAllInputProcessors();
			artists.get(i).removeFromParent();
			artists.remove(i).destroy();
		}
		getCanvas().unregisterAllInputProcessors();
	}

}
